<?php
/**
 * 
 * Detailed information pertaining to one particular run
 * Can also be used to download speed and distance curves with 10 second intervals
 * per sample
 * @author Martin Sommervold (martin@sommervold.com)
 *
 */
class Run{
	/**
	 * 
	 * Unique run ID
	 * @var int
	 */
	public $id;
	
	/**
	 * 
	 * Run start datetime
	 * @var string
	 */
	public $startTime;
	
	/**
	 * 
	 * Run duration, converted
	 * @var string
	 */
	public $duration;
	
	/**
	 * 
	 * Run duration in original data format (microseconds
	 * @var int
	 */
	public $durationOriginal;
	
	/**
	 * 
	 * Distance in km
	 * @var float
	 */
	public $distance;
	
	/**
	 * 
	 * When the data was synced (datetime)
	 * @var string
	 */
	public $syncTime;
	
	/**
	 * 
	 * Calories burned
	 * @var float
	 */
	public $calories;
	
	/**
	 * 
	 * The name of the run template on your ipod/iphone
	 * @var string
	 */
	public $name;
	
	/**
	 * 
	 * Description
	 * @var string
	 */
	public $description;
	
	/**
	 * 
	 * An integer indicating how the run felt, 2 = great, 1 = soso, 0 = bad
	 * @var int
	 */
	public $howFelt;
	
	/**
	 * 
	 * Weather, INT here as welt
	 * @var int
	 */
	public $weather;
	
	/**
	 * 
	 * Same goes for terrain, don't know what the numbers indicate though
	 * @var int
	 */
	public $terrain;
	
	/**
	 * 
	 * Dont have any sample data on this, sorry. Might be related to pulse monitor equipment
	 * @var unknown_type
	 */
	public $intensity;
	
	/**
	 *  
	 * Don't have any data on this either :)
	 * @var unknown_type
	 */
	public $gpxId;
	
	/**
	 * 
	 * An array with floats, each float is the distance traveled after N=10 seconds
	 * @var array
	 */
	public $distanceIntervalData;
	
	/**
	 * 
	 * An array with floats, each representing the current speed at N=10 second intervals
	 * @var array
	 */
	public $speedIntervalData;
	
	/**
	 * 
	 * Curl-object
	 * @var object
	 */
	private $ch;
	
	/**
	 * 
	 * URL for fetching detail data
	 * @var string
	 */
	const detailsUrl = 'https://secure-nikerunning.nike.com/nikeplus/v1/services/app/get_run.jsp?';
	
	/**
	 * 
	 * All parameters except duration are parsed by the parent class
	 * @param $id
	 * @param $startTime
	 * @param $duration
	 * @param $distance
	 * @param $syncTime
	 * @param $calories
	 * @param $name
	 * @param $description
	 * @param $howFelt
	 * @param $weather
	 * @param $terrain
	 * @param $intensity
	 * @param $gpxId
	 * @param $ch
	 * @param $loadDetails
	 */
	function __construct($id, $startTime, $duration, $distance, $syncTime, $calories, $name, $description, $howFelt, $weather, $terrain, $intensity, $gpxId, $ch, $loadDetails = false){
		$this->startTime = $startTime;
		$this->duration = $this->duration($duration);
		$this->durationOriginal = $duration;
		$this->distance = $distance;
		$this->syncTime = $syncTime;
		$this->calories = $calories;
		$this->name = $name;
		$this->description = $description;
		$this->howFelt = $howFelt;
		$this->weather = $weather;
		$this->terrain = $terrain;
		$this->intensity = $intensity;
		$this->gpxId = $gpxId;
		$this->ch = $ch;
		$this->id = $id;
		
		
		/*
		 *  
		 *  This defaults to false due to high resource cost, but can be invoked through 
		 *  construct if one wants to grab all data right away
		 */
		if($loadDetails){
			$this->loadRunDetails();
		}
	}
	
	/**
	 * 
	 * Loads extended distance and speed data into this object
	 */
	public function loadRunDetails(){
	
		$listfile = $this->cache.'/nikeplus_'.$this->login.'_run_details_'.$this->id.'.xml';
		if(is_file($listfile)){
			unlink($listfile);	
		}
        	
        curl_setopt($this->ch, CURLOPT_FILE, $fp = fopen($listfile,'w'));
        curl_setopt($this->ch, CURLOPT_URL, Run::detailsUrl);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, "id=$this->id");
        curl_exec($this->ch); fclose($fp);
        	
       	$rundata = simplexml_load_file($listfile);
       	
       	foreach($rundata->sportsData->extendedDataList->extendedData as $curvedata){
		
       		switch (strtolower($curvedata['dataType'])) {
			    case 'distance':
			        $this->distanceIntervalData = explode(',', $curvedata);
			        break;
			    case 'speed':
			       	$this->speedIntervalData = explode(',', $curvedata);
			        break;
			}
       		
       	}
       	
	}
	
	/**
	 * 
	 * Converts duration miroseconds to a human readable format
	 * @param int $dur
	 */
	private function duration($dur) {
	        $hours   = (int)($dur/3600000);
	        $minutes = (int)(($dur%3600000)/60000);
	        $seconds = (int)((($dur%3600000)%60000)/1000);
	        if($hours) return sprintf("%2d:%02d'%02d\"", $hours, $minutes, $seconds);
	        else return sprintf("%2d'%02d\"", $minutes, $seconds);
	    }
}